﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShiQuan.Hanker
{
    /// <summary>
    /// 海康网络视频录像机
    /// </summary>
    public class HikNVRHelper
    {
        private int _userId = -1;
        /// <summary>
        /// 登录用户标识
        /// </summary>
        public int UserId {
            get { return this._userId; }
            set { this._userId = value; }
        }
        
        private DeviceInfo _deviceInfo = new DeviceInfo();
        /// <summary>
        /// 设备信息
        /// </summary>
        public DeviceInfo DeviceInfo
        {
            get { return this._deviceInfo; }
        }
        
        private List<ChannelInfo> _Channels = new List<ChannelInfo>();
        /// <summary>
        /// 设备通道状态
        /// </summary>
        public List<ChannelInfo> Channels
        {
            get { return this._Channels; }
        }

        private HCNetSDK.NET_DVR_DEVICEINFO_V30 mDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        private HCNetSDK.NET_DVR_IPPARACFG_V40 mIpParaCfgV40 = new HCNetSDK.NET_DVR_IPPARACFG_V40();
        /// <summary>
        /// 获取设备IP配置结构
        /// </summary>
        public HCNetSDK.NET_DVR_IPPARACFG_V40 DVR_IPPARACFG
        {
            get { return this.mIpParaCfgV40; }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Login(string serverIp, int serverPort, string userName, string password, out string msg)
        {
            msg = string.Empty;
            
            this._userId = HikDVRHelper.Login(serverIp, serverPort, userName, password,ref mDeviceInfo, out msg);
            if (this._userId < 0)
                return false;

            /*初始设备信息*/
            this._deviceInfo = new DeviceInfo()
            {
                ServerIP = serverIp,
                ServerPort = serverPort,
                UserName = userName,
                Password = password
            };

            if (HikDVRHelper.GetDeviceInfo(this.UserId, ref _deviceInfo, out msg) == false)
                return false;

            if (HikDVRHelper.GetNetworkInfo(this.UserId, ref this._deviceInfo, out msg) == false)
                return false;

            return HikDVRHelper.GetChannelList(this._userId, mDeviceInfo, mIpParaCfgV40, out _Channels, out msg);
        }
        /// <summary>
        /// 重新刷新通道
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool RefreshChannel(out string msg)
        {
            return HikDVRHelper.GetChannelList(this._userId, mDeviceInfo, mIpParaCfgV40, out _Channels, out msg);
        }
        
        #region 抓拍
        /// <summary>
        /// 抓拍
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Capture(int channelIndex, out string msg)
        {
            msg = string.Empty;
            var filePath = Application.StartupPath + "\\Capture\\" + channelIndex + "";
            if (System.IO.Directory.Exists(filePath) == false)
                System.IO.Directory.CreateDirectory(filePath);
            var fileName = filePath + "\\" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".jpg";
            return this.Capture(channelIndex, fileName, out msg);
        }

        /// <summary>
        /// 抓拍
        /// </summary>
        /// <param name="saveFile"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Capture(int channelIndex, string saveFile, out string msg)
        {
            return HikDVRHelper.Capture(this.UserId, channelIndex, saveFile, out msg);
        }
        #endregion

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Logout(out string msg)
        {
            return HikDVRHelper.Logout(this.UserId, out msg);
        }
        /// <summary>
        /// 销毁资源
        /// </summary>
        public void Dispose()
        {
            if (this.UserId >= 0)
            {
                var msg = string.Empty;
                HikDVRHelper.Logout(this.UserId, out msg);
            }
        }
    }
}
